package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.MPage;
import com.xxx.entity.OrderProduce;
import com.xxx.entity.query.OrderProduceQuery;
import com.xxx.service.IOrderProduceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/orderProduce")
@Tag(name = "订单与农产品多对多管理")
public class OrderProduceController {

    @Autowired
    private IOrderProduceService orderProduceService;

    @PostMapping("/add")
    @Operation(summary = "添加多对多")
    public Result add(@RequestBody OrderProduce orderProduce){
        Boolean flag = orderProduceService.save(orderProduce);
        return flag ? Result.success("添加多对多成功") : Result.error("添加多对多失败");
    }

    @GetMapping("/delete/{orderProduceId}")
    @Operation(summary = "删除多对多")
    public Result delete(@PathVariable Integer orderProduceId){
        Boolean flag = orderProduceService.removeById(orderProduceId);
        return flag ? Result.success("删除多对多成功") : Result.error("不存在该多对多Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改多对多")
    public Result update(@RequestBody OrderProduce orderProduce){
        Boolean flag = orderProduceService.updateById(orderProduce);
        return flag ? Result.success("修改多对多成功") : Result.error("修改多对多失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询多对多")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
//        IPage<OrderProduce> page = orderProduceService.page(new Page<>(current,size));
        MPage page = orderProduceService.selectByPage(current, size);
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据订单Id、农产品Id条件查询多对多")
    public Result selectByCondition(@RequestBody OrderProduceQuery orderProduceQuery){
//        LambdaQueryWrapper<OrderProduce> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(orderProduceQuery.getOrderId()!=null,
//                        OrderProduce::getOrderId,orderProduceQuery.getOrderId())
//                .eq(orderProduceQuery.getProduceId()!=null,
//                        OrderProduce::getProduceId,orderProduceQuery.getProduceId());
//        IPage<OrderProduce> page = orderProduceService.page(new Page<>(orderProduceQuery.getCurrent(), orderProduceQuery.getSize()),
//                lqw);
        MPage page = orderProduceService.selectByCondition(orderProduceQuery);
        return Result.success(page);
    }
}

